package com.zwface.demo.function.attendance;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.lifecycle.ViewModelProviders;

import com.arcsoft.arcfacedemo.common.Constants;
import com.arcsoft.face.ActiveFileInfo;
import com.arcsoft.face.ErrorInfo;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.enums.RuntimeABI;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.zwface.demo.R;
import com.zwface.demo.base.BaseFragment;
import com.zwface.demo.databinding.FragmentAttendanceBinding;
import com.zwface.demo.function.attendance.mvvm.AttendanceViewModel;
import com.zwface.demo.function.leave.LeaveActivity;
import com.zwface.demo.function.leave.LeaveLsActivity;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * created by      张中文
 * on:             2020/4/28 13:37
 * 描述：
 */
public class AttendanceFragment extends BaseFragment<FragmentAttendanceBinding> implements View.OnClickListener {
    private AttendanceViewModel viewModel;
    @Override
    protected int onLayoutId() {
        return R.layout.fragment_attendance;
    }

    @Override
    protected void onInitBinding(FragmentAttendanceBinding binding) {
        super.onInitBinding(binding);
        viewModel = ViewModelProviders.of(this).get(AttendanceViewModel.class);
        binding.setData(viewModel);
    }

    @Override
    protected void onInitComponent() {
        super.onInitComponent();
        initTopbar();
        //学生功能
        binding.llRegisterFace.setOnClickListener(this);
        binding.llSignFace.setOnClickListener(this);
        binding.llLeaveSubmit.setOnClickListener(this);
        //教师功能
        binding.llList.setOnClickListener(this);
        binding.llOverview.setOnClickListener(this);
        //公共功能
        binding.llLeaveList.setOnClickListener(this);
    }

    private void initTopbar(){
        binding.topbar.setTitle("考勤");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ll_register_face:
                activeFaceEngine(true);
                break;
            case R.id.ll_sign_face:
                activeFaceEngine(false);
                break;
            case R.id.ll_list:
                gotoActivityNotClose(AttendanceLsActivity.class,null);
                break;
            case R.id.ll_overview:
                gotoActivityNotClose(AttendanceOverviewActivity.class,null);
                break;
            case R.id.ll_leave_submit:
                gotoActivityNotClose(LeaveActivity.class,null);
                break;
            case R.id.ll_leave_list:
                gotoActivityNotClose(LeaveLsActivity.class,null);
                break;
        }
    }

    /**
     * 激活人脸识别引擎
     */
    private void activeFaceEngine(Boolean isShowRegister){
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) {
                RuntimeABI runtimeABI = FaceEngine.getRuntimeABI();
                LogUtils.d("subscribe: getRuntimeABI() " + runtimeABI);

                long start = System.currentTimeMillis();
                int activeCode = FaceEngine.activeOnline(context, Constants.APP_ID, Constants.SDK_KEY);
                LogUtils.d("subscribe cost: " + (System.currentTimeMillis() - start));
                emitter.onNext(activeCode);
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Integer>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Integer activeCode) {
                        if (activeCode == ErrorInfo.MOK) {
                            //ToastUtils.showShort(getString(com.arcsoft.arcfacedemo.R.string.active_success));
                            Bundle bundle = new Bundle();
                            bundle.putBoolean("isShowRegister",isShowRegister);
                            gotoActivityNotClose(FaceRecognizeActivity.class,bundle);
                        } else if (activeCode == ErrorInfo.MERR_ASF_ALREADY_ACTIVATED) {
                            //ToastUtils.showShort(getString(com.arcsoft.arcfacedemo.R.string.already_activated));
                            Bundle bundle = new Bundle();
                            bundle.putBoolean("isShowRegister",isShowRegister);
                            gotoActivityNotClose(FaceRecognizeActivity.class,bundle);
                        } else {
                            ToastUtils.showShort(getString(com.arcsoft.arcfacedemo.R.string.active_failed, activeCode));
                        }

                        ActiveFileInfo activeFileInfo = new ActiveFileInfo();
                        int res = FaceEngine.getActiveFileInfo(context, activeFileInfo);
                        if (res == ErrorInfo.MOK) {
                            LogUtils.d(activeFileInfo.toString());
                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        ToastUtils.showShort(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
